package com.glsc.ngateway.flowable.controller;

import com.glsc.ngateway.common.api.flowable.dto.demand.FlowableDemandDto;
import com.glsc.ngateway.common.api.flowable.resp.FlowableResponse;
import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableDemand;
import com.glsc.ngateway.flowable.service.FlowableDemandService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/demand")
@Api(value = "flowable需求管理类", tags = {"flowable需求管理类"})
public class DemandController {

    private static Logger logger = LoggerFactory.getLogger(DemandController.class);

    @Autowired
    FlowableDemandService flowableDemandService;


    @RequestMapping(method = RequestMethod.GET, value = "/getDemandInfo")
    public FlowableResponse getDemandInfo(@RequestParam("processId") String processId) throws IOException {
        FlowableDemandDto de = new FlowableDemandDto();
        List<FlowableDemandDto> demands = flowableDemandService.findByProcessInstanceId(processId);
        if (demands != null && demands.size() > 0) {
            de = demands.get(0);
        }
        return FlowableResponse.success(de);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/getDemandInfos")
    public FlowableResponse getDemandInfos(@RequestBody List<String> processIds) throws IOException {
        FlowableDemand de = new FlowableDemand();
        List<FlowableDemand> demands = flowableDemandService.findByProcessInstanceIdIn(processIds);
//        if (demands != null && demands.size() > 0) {
//            de = demands.get(0);
//        }
        return FlowableResponse.success(demands);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/updateEmergeLevel")
    public FlowableResponse updateEmergeLevel(@RequestParam("processId") String processId,
                                              @RequestParam("demandEmergeLevel") String demandEmergeLevel) throws IOException {
        FlowableDemandDto de = new FlowableDemandDto();
        List<FlowableDemandDto> demands = flowableDemandService.findByProcessInstanceId(processId);
        if (demands != null && demands.size() > 0) {
            de = demands.get(0);
            de.setDemandEmergeLevel(demandEmergeLevel);
            flowableDemandService.saveDemandSubmit(de);
        }
        return FlowableResponse.success(de);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/findByVcStaffLikeOrVcOriginalStaffOrDevUserId")
    public FlowableResponse findByVcStaffLikeOrVcOriginalStaffOrDevUserId(@RequestParam(value = "vcStaff" , required = false) String vcStaff,
                                                                          @RequestParam(value = "vcOriginalStaff" , required = false) String vcOriginalStaff,
                                                                          @RequestParam(value = "devUserId" , required = false) String devUserId,
                                                                          @RequestParam(value = "sameDepartmentAccounts", required = false) List<String> sameDepartmentAccounts,
                                                                          @RequestParam(value = "demandDetail", required = false) String demandDetail) throws IOException {
        FlowableDemand de = new FlowableDemand();
        List<FlowableDemand> demands = flowableDemandService.findByVcStaffLikeOrVcOriginalStaffOrDevUserId(vcStaff,vcOriginalStaff,devUserId,sameDepartmentAccounts,demandDetail);
//        if (demands != null && demands.size() > 0) {
//            de = demands.get(0);
//        }
        return FlowableResponse.success(demands);
    }

}
